package com.industrial.app.controller;

import cn.hutool.core.date.DateException;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;

import com.industrial.app.domain.BizManufacturingPmiComparison;
import com.industrial.app.domain.request.BizManufacturingPmiComparisonRequest;
import com.industrial.app.service.IBizManufacturingPmiComparisonService;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;


/**
 * 山东与全国制造业采购经理指数对比Controller
 *
 * @author SangGuangCan
 * @date 2023-12-27
 */
@RestController
@RequestMapping("/biz/comparison")
public class BizManufacturingPmiComparisonController extends BaseController {

    private IBizManufacturingPmiComparisonService bizManufacturingPmiComparisonService;

    @Value("${app.file-upload-path}")
    private String fileUploadPath;

    @Autowired
    public BizManufacturingPmiComparisonController(IBizManufacturingPmiComparisonService bizManufacturingPmiComparisonService){
        this.bizManufacturingPmiComparisonService = bizManufacturingPmiComparisonService;
    }

    /**
     * 查询山东与全国制造业采购经理指数对比列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:comparison:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizManufacturingPmiComparison bizManufacturingPmiComparison)
    {
        startPage();
        List<BizManufacturingPmiComparison> list = bizManufacturingPmiComparisonService.selectBizManufacturingPmiComparisonList(bizManufacturingPmiComparison);
        return getDataTable(list);
    }

    /**
     * 导出山东与全国制造业采购经理指数对比列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:comparison:export')")
//    @Log(title = "山东与全国制造业采购经理指数对比", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizManufacturingPmiComparison bizManufacturingPmiComparison)
    {
        List<BizManufacturingPmiComparison> list = bizManufacturingPmiComparisonService.selectBizManufacturingPmiComparisonList(bizManufacturingPmiComparison);
        ExcelUtil<BizManufacturingPmiComparison> util = new ExcelUtil<BizManufacturingPmiComparison>(BizManufacturingPmiComparison.class);
        util.exportExcel(response, list, "山东与全国制造业采购经理指数对比数据");
    }

    /**
     * 获取山东与全国制造业采购经理指数对比详细信息
     */
//    @PreAuthorize("@ss.hasPermi('biz:comparison:query')")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(bizManufacturingPmiComparisonService.selectBizManufacturingPmiComparisonByBaseId(baseId));
    }

    /**
     * 新增山东与全国制造业采购经理指数对比
     */
//    @PreAuthorize("@ss.hasPermi('biz:comparison:add')")
//    @Log(title = "山东与全国制造业采购经理指数对比", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizManufacturingPmiComparison bizManufacturingPmiComparison)
    {
        return toAjax(bizManufacturingPmiComparisonService.insertBizManufacturingPmiComparison(bizManufacturingPmiComparison));
    }

    /**
     * 修改山东与全国制造业采购经理指数对比
     */
//    @PreAuthorize("@ss.hasPermi('biz:comparison:edit')")
//    @Log(title = "山东与全国制造业采购经理指数对比", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizManufacturingPmiComparison bizManufacturingPmiComparison)
    {
        return toAjax(bizManufacturingPmiComparisonService.updateBizManufacturingPmiComparison(bizManufacturingPmiComparison));
    }

    /**
     * 删除山东与全国制造业采购经理指数对比
     */
//    @PreAuthorize("@ss.hasPermi('biz:comparison:remove')")
//    @Log(title = "山东与全国制造业采购经理指数对比", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(bizManufacturingPmiComparisonService.deleteBizManufacturingPmiComparisonByBaseIds(baseIds));
    }

    /**
     * <p>
     *     导入山东与全国制造业采购经理指数对比Excel
     * </p>
     * @param file 接收到前端传来的Excel文件
     * @return 返回导入成功或都失败
     */
//    @Anonymous
    @PostMapping("/importManufacturingPmiComparison")
    public AjaxResult importManufacturingPmiComparison(@RequestPart("file") MultipartFile multipartFile){
        try{
            if(multipartFile.isEmpty()){
                return AjaxResult.warn("没有找到需要上传的文件");
            }
            if(!StrUtil.containsAny(FileNameUtil.getSuffix(multipartFile.getOriginalFilename()),"xls","xlsx")){
                return AjaxResult.warn(String.format("上传文件必须为Excel表格文件"));
            }


            String absoluteExcelPath = fileUploadPath + File.separator + IdUtil.simpleUUID() + ".xlsx";
            File file = new File(absoluteExcelPath);
            if(!file.exists()){
                FileUtil.touch(file);
            }
            multipartFile.transferTo(file);

//            String uploadPath = FileUploadUtils.upload(AppConfig.getProfile() + File.separator + "upload" + File.separator + "manufacturingPmiComparison", file);
//            String truePath = AppConfig.getProfile() + StrUtil.removePrefix(uploadPath,"/profile");
            bizManufacturingPmiComparisonService.importManufacturingPmiComparison(absoluteExcelPath);
            logger.info("导入Excel文件路径:{}",absoluteExcelPath);
        }catch (Exception e){
            logger.error("文件导入失败:{}",e.getMessage());
            return AjaxResult.error(String.format("文件导入失败:{}",e.getMessage()));
        }
        return AjaxResult.success();
    }

    /**
     * <p>
     *     查询APP左上角的日期数据
     * </p>
     * @return 日期的集合
     */
//    @Anonymous
    @GetMapping("/getUniqueDatesFromDatabase")
    public AjaxResult getUniqueDatesFromDatabase(){
        List<String> dateArgs = bizManufacturingPmiComparisonService.getUniqueDatesFromDatabase();
        return AjaxResult.success(dateArgs);
    }

    /**
     * 查询指定日期前一年的数据 表格
     * @param bizManufacturingPmiComparisonRequest 请求类
     * @return
     */
//    @Anonymous
    @GetMapping("/getDataOneYearBeforeSpecificDateTable")
    public AjaxResult getDataOneYearBeforeSpecificDateTable(BizManufacturingPmiComparisonRequest bizManufacturingPmiComparisonRequest){
        if(StringUtils.isBlank(bizManufacturingPmiComparisonRequest.getDates())){
            return AjaxResult.warn("没有获取到指定日期");
        }
        try{
            DateUtil.parse(bizManufacturingPmiComparisonRequest.getDates(),"yyyy年MM月");
            List<BizManufacturingPmiComparison> result = bizManufacturingPmiComparisonService.getDataOneYearBeforeSpecificDate(bizManufacturingPmiComparisonRequest);
            return AjaxResult.success(result);
        }catch (DateException de){
            logger.error("日期格式校验失败,传入日期:{},正确格式:{}",bizManufacturingPmiComparisonRequest.getDates(),"yyyy年MM月");
            return AjaxResult.error("日期校验失败");
        }
    }

    @GetMapping("/getDataOneYearBeforeSpecificDateEcharts")
    public  AjaxResult getDataOneYearBeforeSpecificDateEcharts(BizManufacturingPmiComparisonRequest bizManufacturingPmiComparisonRequest){
        if(StringUtils.isBlank(bizManufacturingPmiComparisonRequest.getDates())){
            return AjaxResult.warn("没有获取到指定日期");
        }
        try{
            DateUtil.parse(bizManufacturingPmiComparisonRequest.getDates(),"yyyy年MM月");
            return AjaxResult.success(bizManufacturingPmiComparisonService.getDataOneYearBeforeSpecificDateEcharts(bizManufacturingPmiComparisonRequest));
        }catch (DateException de){
            logger.error("日期格式校验失败,传入日期:{},正确格式:{}",bizManufacturingPmiComparisonRequest.getDates(),"yyyy年MM月");
            return AjaxResult.error("日期校验失败");
        }
    }

    /**
     * <p>
     *     将临时表中的数据迁移到正式表虽
     * </p>
     * @return
     */
//    @Anonymous
    @PostMapping("/transferDataInOneClick")
    public AjaxResult transferDataInOneClick(){
        return toAjax(bizManufacturingPmiComparisonService.transferDataInOneClick());
    }

    /**
     * <p>
     *     清空所有的山东与全国制造业采购经理指数对比数据
     * </p>
     * @return
     */
    @DeleteMapping("/clearAllTableData")
    public AjaxResult clearAllTableData(){
        bizManufacturingPmiComparisonService.clearAllTableData();
        return AjaxResult.success();
    }
}

